# coding: utf-8

from google.appengine.ext import db
import bs4
import core.utility
import logging
import urllib2
import data.model
import datetime

class CrawlProducts:
    def crawlSocialProducts(self, social_provider, social_code, xml_url):
        try:
            retData = urllib2.urlopen(xml_url).read()
        except urllib2.HTTPError, e:
            logging.error("Http Error: %d" % e.code)
            return -1
        except urllib2.URLError, e:
            logging.error("Network error: %s" % e.reason.args[1])
            return -2
    
        insert_count = 0    # 신규로 입력된 상품 갯수
        
        soup = bs4.BeautifulSoup(retData)
        for item in soup.findAll('item'):       # XML 파일에서 'item'에 해당하는 데이터를 순차적으로 조회
            if self.exist(item.title.string):   # 이미 동일한 정보가 입력되었는지 확인
                if item.sell_count != None:
                    # 업데이트한 날짜가 동일한 경우에는 갱신하지 않음
                    q = db.GqlQuery("SELECT * FROM Products WHERE title = :1", 
                                    core.utility.Utility.convertEncoding(item.title.string))
                    p = q.fetch(1)[0]
                    if datetime.date(p.latest_update.year, 
                                     p.latest_update.month, 
                                     p.latest_update.day) == datetime.date.today():
                        continue
                    
                    # 일일 판매량을 갱신
                    p.sell_list.append(int(item.sell_count.string))
                    
                    # 직전 판매 증가량 갱신
                    listSize = len(p.sell_list)
                    if listSize == 1:
                        p.sell_delta = p.sell_list[0]
                    else:
                        p.sell_delta = p.sell_list[listSize-1] - p.sell_list[listSize-2]
                    
                    # 데이터스토어에 수정한 내용을 반영
                    p.put()
                    continue
                
            # 새롭게 등록되는 상품인 경우
            p = data.model.Products()
                   
            if item.title != None:
                p.title = core.utility.Utility.convertEncoding(item.title.string)
            
            if item.time_start != None:
                p.time_start = datetime.datetime.strptime(item.time_start.string, 
                                                          "%Y-%m-%d %H:%M:%S")
                
            if item.time_end != None:
                p.time_end = datetime.datetime.strptime(item.time_end.string, 
                                                        "%Y-%m-%d %H:%M:%S")
                # 0 - Available, 1-Expired  
                if (p.time_end - datetime.datetime.today()):
                    p.is_expired = 0
                else:
                    p.is_expired = 1
                    
            if item.link.string != None:
                p.link = core.utility.Utility.convertEncoding(item.link.string)
            
            if item.price_original != None:
                p.price_original = int(item.price_original.string)
                
            if item.price_now != None:
                p.price_now = int(item.price_now.string)
                
            if item.sale_percent != None:
                p.sale_percent = int(item.sale_percent.string)
            if item.photo1 != None:
                p.photo_url = core.utility.Utility.convertEncoding(item.photo1.string)
                        
            if item.area != None:
                p.area = core.utility.Utility.convertEncoding(item.area.string)
          
            if item.type != None:            
                p.type = core.utility.Utility.convertEncoding(item.type.string)
                
            if item.category != None:
                p.category = core.utility.Utility.convertEncoding(item.category.string)
            
            if social_provider != None:
                p.social_provider = social_provider
            if social_code != None:
                p.social_provider_code = social_code
                
            if item.sell_count != None:
                p.sell_list = [int(item.sell_count.string)]
                p.sell_delta = int(item.sell_count.string)
            else:
                p.sell_list = []
                p.sell_delta = 0

            # 검색할 키워드 등록
            keywords = []
            keywords.extend(core.utility.Utility.makeKeywordList(item.title.string))
            p.keywords = keywords
            
            p.latest_update = datetime.datetime.now()
            
            p.put()     # 데이터스토어에 객체를 저장
            insert_count+=1
                   
        return insert_count
    
    def exist(self, itemTitle):
        q = db.GqlQuery("SELECT * FROM Products WHERE title = :1", 
                        core.utility.Utility.convertEncoding(itemTitle))                    
        result = q.fetch(1)
        if not result:
            return False
        else:
            return True
